/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.blitzcoder.collide.gui;

import java.awt.Cursor;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JScrollPane;

/**
 *
 * @author blitzcoder
 */
public class ImageDocument extends Document implements MouseListener, MouseMotionListener {

    private File file;
    private JScrollPane scroller;
    private Point dragStart;

    public ImageDocument(File file) {

        this.file = file;
        
        if (!file.exists())
            throw new Error("File does not exist!");

    }

    @Override
    public void load() throws Exception{
        ImageIcon icon = new ImageIcon(file.getAbsolutePath());

        if (icon == null) {
            throw new Exception("Error loading Image");
        }

        JLabel label = new JLabel();
        label.setIcon(icon);

        scroller = new JScrollPane(label);

        setLayout(new GridLayout());
        add(scroller);
        
        scroller.addMouseListener(this);
        scroller.addMouseMotionListener(this);

    }

    @Override
    public String getTitle() {
        return file.getName();
    }

    @Override
    public Icon getIcon() {
        return de.blitzcoder.collide.icons.Icon.load("file_image.png");
    }

    @Override
    public String getToolTip() {
        return file.getAbsolutePath();
    }

    @Override
    public boolean equalsFile(File file) {
        return this.file.equals(file);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        dragStart = e.getPoint();
        scroller.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        scroller.setCursor(Cursor.getDefaultCursor());
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        int x = scroller.getHorizontalScrollBar().getValue();
        int y = scroller.getVerticalScrollBar().getValue();

        x += dragStart.x - e.getX();
        y += dragStart.y - e.getY();

        scroller.getHorizontalScrollBar().setValue(x);
        scroller.getVerticalScrollBar().setValue(y);

        dragStart = e.getPoint();
    }

    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseMoved(MouseEvent e) {}


}